import ugLang from '/common/lang/locale-ug_CN'
import zhLang from '/common/lang/locale-zh_CN'
import { loginApi } from "./request/api.js";
import axios from './request/axios-miniprogram';

App({
  globalData: {
    userInfo: null,
    lang: null,
    NETWORK_LIB: '',
    isIphone: false,
    API_URL: 'https://api.atush.vip',
    // API_URL: 'http://127.0.0.1:3005'
  },

  changeLanguage(langName) {
    if (langName == 'ugLang') {
      wx.setStorageSync('currLangName', langName)
      this.globalData.lang = ugLang
      return
    }
    if (langName == 'zhLang') {
      wx.setStorageSync('currLangName', langName)
      this.globalData.lang = zhLang
    }
  },

  // 请求头配置：发起请求时添加该方法为参数
  header() {
    let userInfo = wx.getStorageSync('userInfo')
    let token = userInfo != null? userInfo.token:null
    let header = { "Content-Type": "application/json;charset=utf-8","token":token}
    // let sid = wx.getStorageSync('sid')
    // if (sid.length != 0) header['Cookie'] = sid
    return header
  },

  
  // 登录
  login(callback) {
    if (!callback) callback = function () {}
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      callback(userInfo)
      return
    }
    let code = null;
    wx.login({
      success(res) {
        if (res.code) code = res.code
      }
    })
    
    wx.getUserProfile({
      desc: '授权登录吧！',
      success: (res) => {
        wx.request({
          url: `${this.globalData.API_URL}/userLogin`,
          header: {
            'content-type': 'application/json'
          },
          method: 'post',
          data: {
            code: code,
            iv: res.iv,
            encryptedData: res.encryptedData
          },
          success: (res) =>{
            this.userInfo = res.data.user
            wx.setStorageSync('uid', res.data.user.id);
            wx.setStorage({
              key: "userInfo",
              data: res.data,
            })
            callback(res.data)
          },
          fail:err=>{
            console.log('err: ', err);
          }
        })
      },
      fail: err => {
        // 返回上页面
        // wx.navigateBack();
      }
    })
  },

  // 检查登录是否过期
  isOvedue() {
    let user_id = wx.getStorageSync('uid')

    if (user_id && user_id > 0) {
      wx.request({
        url: `${this.globalData.API_URL}/getFavorites`,
        data: { user_id },
        header: this.header(),
        method: 'post',
        success: function (res) {
          if (res.data.status == 523) {
            wx.setStorage({
              key: "userInfo",
              data: '',
            })
          }
        }
      })
    }
  },

  onLaunch() {
    // 获取机型信息
    const { statusBarHeight, platform } = wx.getSystemInfoSync()
    const { top, height } = wx.getMenuButtonBoundingClientRect()

    // 判断是否苹果产品
    wx.getSystemInfo({
      success: res => {
        const model = res.model.substring(0, 6)
        if (model == 'iPad' || model == 'iPhone') {
          this.globalData.isIphone = true
        }
      }
    });

    // 状态栏高度
    wx.setStorageSync('statusBarHeight', statusBarHeight)
    // 胶囊按钮高度 一般是32 如果获取不到就使用32
    wx.setStorageSync('menuButtonHeight', height ? height : 32)

    // 判断胶囊按钮信息是否成功获取
    if (top && top !== 0 && height && height !== 0) {
      const navigationBarHeight = (top - statusBarHeight) * 2 + height
      // 导航栏高度
      wx.setStorageSync('navigationBarHeight', navigationBarHeight)
    } else {
      wx.setStorageSync(
        'navigationBarHeight',
        platform === 'android' ? 48 : 40
      )
    }

    // 检查登录是否过期
    this.isOvedue()
  },

  onShow() {

    let langName = wx.getStorageSync('currLangName')
    // console.log("current lang: ", langName);
    if (!langName) {
      this.globalData.lang = ugLang
    } else {
      this.changeLanguage(langName)
    }
  },

    // 小程序退出时   
  onHide() {}
})
